#define _CRT_SECURE_NO_WARNINGS 1
#include"Heap.h"


void test()
{
	HP hp;
	HeapInit(&hp);
	HeapPush(&hp, 1);
	HeapPush(&hp, 2);
	HeapPush(&hp, 3);
	HeapPush(&hp, 4);
	HeapPrint(&hp);
	HeapPop(&hp);
	HeapPop(&hp);
	HeapPop(&hp);
	HeapPop(&hp);
	HeapPrint(&hp);

	HeapDestroy(&hp);
}

void PrintTopK(int* a, int n, int k)
{
	HP hp;
	HeapInit(&hp);
	int i = 0;
	for (i = 0; i < k; i++)
	{
		HeapPush(&hp, a[i]);
	}
	for (i = k; i < n; i++)
	{
		if (a[i] > HeapTop(&hp))
		{
			(&hp)->a[0] = a[i];
			HeapTopAdjust(&hp);
		}
	}
	HeapPrint(& hp);
	HeapDestroy(&hp);
}

void testTopK()
{
	int n = 10000;
	int k = 9;
	int* a = (int*)malloc(sizeof(int)*n);
	assert(a);
	srand((unsigned)time(NULL));
	for (int i = 0; i < n; i++)
	{
		a[i] = rand() % 1000000;
	}

	a[11] = 1000000 + 1;
	a[5231] = 1000000 + 2;
	a[2345] = 1000000 + 3;
	a[3567] = 1000000 + 4;
	a[1298] = 1000000 + 5;
	a[67] = 1000000 + 6;
	a[7896] = 1000000 + 7;
	a[5748] = 1000000 + 8;
	a[9898] = 1000000 + 9;
	PrintTopK(a, n, 9);
	
}

int main()
{
	//test();
	testTopK();
	return 0;
}

